<?php

/**
 * CCoreMimeMessageAttachment
 * CCoreMimeMessageAttachmentsCollection
 *
 * @author Usenko Timur <usenko.timur@gmail.com>
 * @package mime
 */

CCore::Using('base.collection');
CCore::Using('mime.part');

/**
 * @package mime
 */
class CCoreMimeMessageAttachment
{
	/**
	 * @var CCoreMimePart
	 */
	private $_oMimePart;
	
	/**
	 * @paran CCoreMimePart $oMimePart
	 * @return CCoreMimeMessageAttachment
	 */
	public function __construct(CCoreMimePart &$oMimePart)
	{
		$this->_oMimePart =& $oMimePart;
	}

	/**
	 * @param string $sOutputCharset = C_MIME_CHARSET_UTF8
	 * @return string
	 */
	public function Name($sOutputCharset = C_MIME_CHARSET_UTF8)
	{
		$sFileName = trim($this->_oMimePart->Headers->GetParamDecodedValueFromHeader(
			C_MIME_HEADERS_CONTENT_DISPOSITION, 'filename', $sOutputCharset));

		if (empty($sFileName))
		{
			$sFileName = trim($this->_oMimePart->Headers->GetParamDecodedValueFromHeader(
				C_MIME_HEADERS_CONTENT_TYPE, 'name', $sOutputCharset));

			if (empty($sFileName))
			{
				$sContentTypeFirstParamValue =
					strtolower($this->_oMimePart->Headers->GetValueByName(C_MIME_HEADERS_CONTENT_TYPE));

				$sFileName = 'attachment';
				if (false !== strpos($sContentTypeFirstParamValue, 'image'))
				{
					$sFileName = str_replace(array('/', '\\'), '.', $sContentTypeFirstParamValue);
				}
				else if (false !== strpos($sContentTypeFirstParamValue, 'message'))
				{
					$sFileName = 'message.eml';
				}
			}
		}

		return $sFileName;
	}

	/**
	 * @return int
	 */
	public function EstimatedSize()
	{
		return $this->_oMimePart->EstimatedSize();
	}

	/**
	 * @return string
	 */
	public function InlineContentIdValue()
	{
		$sHeaderContentIdValue = trim($this->_oMimePart->Headers->GetValueByName(C_MIME_HEADERS_CONTENT_ID));
		if (strlen($sHeaderContentIdValue) > 2)
		{
			$sHeaderContentIdValue = trim($sHeaderContentIdValue, '<>');
		}
		return $sHeaderContentIdValue;
	}

	/**
	 * @param bool $bThrowContentTypeHeader = false
	 * @return void
	 */
	public function PassthruBinaryBody($bThrowContentTypeHeader = false)
	{
		if ($bThrowContentTypeHeader)
		{
			$sName = $this->Name(C_MIME_CHARSET_UTF8);
			if (strlen($sName))
			{
				@header('Content-Disposition: attachment; filename="'.$sName.'"; charset='.C_MIME_CHARSET_UTF8);
				@header('Content-Transfer-Encoding: binary');
			}
		}

		$this->_oMimePart->PassthruBinaryBody($bThrowContentTypeHeader);
	}

	/**
	 * @return resourse
	 */
	public function GetBodyStream()
	{
		return $this->_oMimePart->Body;
	}
	
	/**
	 * @return resourse
	 */
	public function GetBinaryBodyStream()
	{
		return $this->_oMimePart->GetBinaryBodyStream();
	}
}

/**
 * @package mime
 */
class CCoreMimeMessageAttachmentsCollection extends ACoreBaseCollection
{
	/**
	 * @return CCoreMimeMessageAttachmentsCollection
	 */
	public function __construct()
	{
		parent::__construct();
	}

	/**
	 * @param CCoreMimeMessageAttachment $oMimeMessageAttachment
	 * @return void
	 */
	public function Add(CCoreMimeMessageAttachment $oMimeMessageAttachment)
	{
		CCore::CheckTypes(array('$oMimeMessageAttachment'));

		parent::Add($oMimeMessageAttachment);
	}
}